const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: [], //渲染的列表
    current: 1,  //请求的页码
    currtab: 0,
    swipertab: [{
      name: '订单消息', orderStatus: 0
    }, { name: '平台消息', orderStatus: 1 }]
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.messageShow()
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.messageShow()
    
  },
  onShow:function(){
    this.getDeviceInfo()
    this.messageShow()
  },

  messageShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.messageList()
        break
      case 1:
        that.kefuList()
        break
      
    }
  },
  /////后台信息
  kefuList:function(){
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/board/getreply',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        var list = res.data.boardList;

        console.log("list", list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            boardList: list
          });
        }
      }
    })

  },
  // 渲染数据方法
  messageList: function () {

    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/send/allsends',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        var list = res.data.sends;

        console.log("list", list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            sendsList: list
          });
        }
      }
    })
  },
  // 点击任意一条消息后小圆点消失
  viewDetails: function (e) {
    console.log(e)
    var that = this
    console.log("当前的信息ID：" + e.currentTarget.dataset.sendid)
    //发送请求给后台，告诉他用户点击的这条消息状态要改变
    wx.request({
      url: app.globalData.baseUrl +'/send/setreadstatus',
      method: "POST",
      data: {
        "sendId": e.currentTarget.dataset.sendid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },      
      success: function (res) {
        console.log(res)
        wx.navigateTo({
          url: '../order/orderdetail/orderdetail?orderId=' + e.currentTarget.dataset.orderid
        })
      }
    })
    that.messageList()  //调用渲染方法
  },
  // 点击任意一条消息后小圆点消失 平台消息跳转
  viewBoardReply: function (e) {
    console.log(e)
    var that = this
    console.log("当前的信息ID：" + e.currentTarget.dataset.boardid)
    console.log("当前的信息ID：" + e.currentTarget.dataset.usernickname)
    console.log("当前的信息ID：" + e.currentTarget.dataset.usermessages)
    console.log("当前的信息ID：" + e.currentTarget.dataset.reply)
    console.log("当前的信息ID：" + e.currentTarget.dataset.createtime5)
    console.log("当前的信息ID：" + e.currentTarget.dataset.updatetime5)
    //////////////
    /*var boardid=e.currentTarget.dataset.boardid
    var usernickname=e.currentTarget.dataset.usernickname
    var usermessages=e.currentTarget.dataset.usermessages
     var =e.currentTarget.dataset.reply
     var =e.currentTarget.dataset.createtime5
     var =e.currentTarget.dataset.updatetime5*/
    //发送请求给后台，告诉他用户点击的这条消息状态要改变
    wx.request({
      url: app.globalData.baseUrl +'/board/setreadstatus',
      method: "POST",
      data: {
        "boardId": e.currentTarget.dataset.boardid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        wx.navigateTo({
          url: '../message/reply/reply?boardId='   + e.currentTarget.dataset.boardid 
          /*url: '../message/reply/reply?userNickname=' + e.currentTarget.dataset.usernickname + '&boardId=' + e.currentTarget.dataset.boardid + '&userMessages=' + e.currentTarget.dataset.usermessages + '&reply' + e.currentTarget.dataset.reply + '&createTime5' + e.currentTarget.dataset.createtime5 + '&updateTime5' + e.currentTarget.dataset.updatetime5,*/
        })
        
      }
    })
    that.kefuList()  //调用渲染方法
    console.log("当前的信息ID：" + e.currentTarget.dataset.boardid)
    console.log("当前的信息ID：" + e.currentTarget.dataset.usernickname)
    console.log("当前的信息ID：" + e.currentTarget.dataset.usermessages)
    console.log("当前的信息ID：" + e.currentTarget.dataset.reply)
    console.log("当前的信息ID：" + e.currentTarget.dataset.createtime5)
    console.log("当前的信息ID：" + e.currentTarget.dataset.updatetime5)
  },


  deleteit:function(e){
    var that = this;
    console.log("sendId:" + e.currentTarget.dataset.sendid)
    wx.showModal({
      title: '提示',
      content: '确定要取消该消息吗？',

      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/send/deletesend',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: { "sendId": e.currentTarget.dataset.sendid },



            success: function (res) {
              var result = res.data.success;
              var toastText = "删除成功！";
              if (result != true) {
                toastText = "删除失败！";
              } else {

                that.messageList()  //调用渲染方法
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })
        }
      }
    })


  },

  deleteboard:function(e){
    var that = this;
    console.log("boardId:" + e.currentTarget.dataset.boardid)
    wx.showModal({
      title: '提示',
      content: '确定要取消该消息吗？',

      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/board/messagesdelete',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: { "boardId": e.currentTarget.dataset.boardid },



            success: function (res) {
              var result = res.data.success;
              var toastText = "删除成功！";
              if (result != true) {
                toastText = "删除失败！";
              } else {

                that.kefuList()  //调用渲染方法
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })
        }
      }
    })

  }

  
})

